﻿//----------------------------------------------------

#region 命名空间

//    版权：
//    作者：
//建立时间：2012-09-11 00:00:33
//    描述：
//----------------------------------------------------
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using WinnerCMS.Common;
using WinnerCMS.Data;
using WinnerCMS.Model;

#endregion

namespace WinnerCMS.BLL
{
    /// <summary>
    /// 业务逻辑类 WinnerCMS_IPBlackList 的摘要说明。
    /// </summary>
    public class IPBlackListManage
    {
        private static readonly IPBlackListDal MyDAL = new IPBlackListDal();

        #region 添加

        /// <summary>
        /// 添加
        /// </summary>
        public Result Add(IPBlackList model)
        {
            Result res = new Result();
            if (model.IPStart.Length == 0)
            {
                res.Errors = "“起始ＩＰ”不能为空！";
                return res;
            }
            if (model.IPEnd.Length == 0)
            {
                res.Errors = "“结束ＩＰ”不能为空！";
                return res;
            }
            if (model.AdminName.Length == 0)
            {
                res.Errors = "“管理员”不能为空！";
                return res;
            }

            foreach (string str in model.IPStart.Split('.'))
            {
                if (int.Parse(str) > 255)
                {
                    res.Errors = "IP格式错误！";
                    return res;
                }
            }
            foreach (string str in model.IPEnd.Split('.'))
            {
                if (int.Parse(str) > 255)
                {
                    res.Errors = "IP格式错误！";
                    return res;
                }
            }

            model.SectorStart = IP2Location.IpToInt(model.IPStart);
            model.SectorEnd = IP2Location.IpToInt(model.IPEnd);


            if (model.Id == 0)
            {
                MyDAL.Add(model);
            }
            else
            {
                MyDAL.Edit(model);
            }
            ReSet();

            return res;
        }

        #endregion

        #region 删除

        /// <summary>
        /// 删除
        /// </summary>
        public void Delete(string IDs, UpdatePanel UpdatePanel1)
        {
            if (string.IsNullOrEmpty(IDs))
            {
                MsgBox.Message("至少选择一项。", UpdatePanel1);
            }
            else
            {
                MyDAL.Delete(MyConvert.ConvertToArr(IDs));

                ReSet();
            }
        }

        #endregion

        #region 取得黑名单

        private static IList<IPBlackList> _IPBlackList;
        private static readonly object lockobj = new object();

        /// <summary>
        /// 取得黑名单
        /// </summary>
        /// <returns></returns>
        public static IList<IPBlackList> GetBlackList()
        {
            if (_IPBlackList == null)
            {
                lock (lockobj)
                {
                    if (_IPBlackList == null)
                    {
                        _IPBlackList = MyDAL.GetBlackList();
                    }
                }
            }
            return _IPBlackList;
        }

        #endregion

        /// <summary>
        /// 重置IP黑名单数据
        /// </summary>
        private void ReSet()
        {
            _IPBlackList = null;
        }

        /// <summary>
        /// 绑定数据
        /// </summary>
        /// <param name="pp"></param>
        public IList<IPBlackList> BindList(PagingParam pp)
        {
            return MyDAL.BindList(pp);
        }
    }
}